package com.xudow.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.activeshare.edu.ucenter.models.base.SysSubject;
import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItem;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItemAdapter;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems;
import com.xudow.app.R;
import com.xudow.app.ui.task.SubjectsTask;
import com.xudow.app.util.Maps;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 错题本Activity
 *
 * @author lukecheng
 */
public class ClazzMistakeActivity extends BaseActivity {

    private String studentId;

    private List<SysSubject> subjects;
    private FragmentPagerItemAdapter tabAdapter;

    private SmartTabLayout viewPagerTab;
    private ViewPager viewPager;
    private RelativeLayout emptyLayout;

    private SubjectsTask subjectsTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clazz_mistake);

        studentId = getIntent().getStringExtra("studentId");

        viewPagerTab = (SmartTabLayout) findViewById(R.id.viewpager_indicator_view);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        emptyLayout = (RelativeLayout) findViewById(R.id.enroll_require_prompt);
    }

    public void onBackBtnClick(View view) {
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();

        //只有学生才有错题
        if (null == studentId) {
            initEmptyView();
            return;
        }

        showLodingDialog(getString(R.string.loading));

        HashMap<String, String> params = Maps.newHashMap();
        params.put("studentid", studentId);

        subjectsTask = new SubjectsTask(this, subjectsTaskHandler);
        addTask(subjectsTask);
        subjectsTask.execute(params);
    }

    Handler subjectsTaskHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            hideLoadingDialog();

            if (SubjectsTask.SUCCESS == msg.what) {
                SysSubject[] subjectsArr = (SysSubject[]) msg.getData().getSerializable("subjects");

                subjects = new ArrayList<SysSubject>();
                subjects.addAll(Arrays.asList(subjectsArr));

                onSubjectSuccess();
            }
        }
    };

    private void onSubjectSuccess() {
        if (subjects.size() <= 0) {
            initEmptyView();
            return;
        }

        emptyLayout.setVisibility(View.GONE);
        viewPagerTab.setVisibility(View.VISIBLE);
        findViewById(R.id.mistake_add_btn).setVisibility(View.VISIBLE);

        FragmentPagerItems pages = new FragmentPagerItems(this);
        for (SysSubject subject : subjects) {
            Bundle data = new Bundle();
            data.putString("studentId", studentId);
            data.putString("subjectName", subject.getName());

            pages.add(FragmentPagerItem.of(subject.getName(), ClazzMistakeFragment.class, data));
        }

        tabAdapter = new FragmentPagerItemAdapter(
                getSupportFragmentManager(), pages);

        viewPager.setAdapter(tabAdapter);
        viewPagerTab.setViewPager(viewPager);
    }

    /**
     * 添加新错题
     *
     * @param view
     */
    public void addNewMistake(View view) {
        Intent intent = new Intent(this, ClazzMistakeDetailActivity.class);
        intent.putExtra("studentId", studentId);
        intent.putExtra("subjectName", "");
        intent.putExtra("method", "create");

        startActivity(intent);
    }

    private void initEmptyView() {
        emptyLayout.setVisibility(View.VISIBLE);
        viewPagerTab.setVisibility(View.GONE);
    }

}
